$(function () {

  const hello = $('.hello');
  const Admit = $('.navAdmit');
  const Reg = $('.navReg');
  const modify = $('.modify');
  const oUser = $('.username');
  const oPwd = $('.password');
  let tipOne = $('.error1');
  let oBtn = $('.register');
  const log = $('.log');
  let isPwd = false;

  // 1.1获取谁在登录
  let admit = localStorage.getItem('someone');
  // admit ? hello.html(admit) : hello.html('考拉欢迎你');
  // 1.3处理登录后事件(显示登录名等)
  if (admit) {
    hello.html(admit);
    Admit.hide();
    Reg.hide();

    // 2.切换修改密码
    modify.click(function () {
      modify.toggleClass('active');
      $('.o-bg').toggle();
      $('.mo').toggle();
      $('.lo').hide();

      oUser.val(admit);
      oUser.prop("disabled", "true");

      // 3.密码框的判断
      oPwd.on('input', function () {
        console.log(666);
        let pas = oPwd.val().trim();
        if (!/./.test(pas)) {
          tipOne.html('密码不能为空');
          tipOne.css('color', 'red');
          isPwd = false;
        } else if (!/^.{4,10}$/.test(pas)) {
          tipOne.html('密码长度不对');
          tipOne.css('color', 'red');
          isPwd = false;
        } else if (!/^[a-zA-Z0-9_]{4,10}$/.test(pas)) {
          tipOne.html('只能由数字字母下划线组成');
          tipOne.css('color', 'red')
          isPwd = false;
        } else {
          let flag1 = 0, flag2 = 0, flag3 = 0;
          if (/\d/.test(pas)) {
            flag1 = 1;
          }
          if (/[a-z]/.test(pas)) {
            flag2 = 1;
          }
          if (/[A-Z]/.test(pas)) {
            flag3 = 1;
          }
          let res = flag1 + flag2 + flag3;
          if (res === 1) {
            tipOne.html('密码强度弱');
          }
          else if (res === 2) {
            tipOne.html('密码强度中');
          }
          else if (res === 3) {
            tipOne.html('密码强度高');
          }
          tipOne.css('color', 'green');
          isPwd = true;
        }
        if (isPwd) {
          oBtn.prop({ 'disabled': false });
        } else {
          oBtn.prop({ 'disabled': true });

        }
      }
      )

      // 4.修改密码的点击事件
      oBtn.click(function () {
        let username = oUser.val();
        let password = oPwd.val();
        localStorage.setItem(username, password);
        if (isPwd) {
          oBtn.prop({ 'disabled': false });
          // 1.数据请求
          modi({
            username,
            password,
          }).then(res => {
            console.log(res);
            const { status, msg } = res;
            if (status) {
              alert('密码修改成功');
              location.href = '../html/3.login.html';
            } else {
              alert(msg);
            }
          })
        } else {
          oBtn.prop({ 'disabled': true });
        }
      })

    })


    // 5.注销登录
    log.click(function () {
      $('.o-bg').toggle();
      $('.mo').hide();
      $('.lo').toggle();
      logName({
        username: admit
      }).then(res => {
        const { status, msg } = res;
        if (status) {
          localStorage.removeItem(admit);
          localStorage.removeItem('someone');
          alert('注销成功')
          location.href = '../html/2.register.html'
        } else {
          alert(msg);
        }
      })








    })




  }



})




